package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.system.domain.SocialSecurity;
import com.ruoyi.system.mapper.SocialSecurityMapper;
import com.ruoyi.system.service.ISocialSecurityService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.validation.Validator;

/**
 * 居民医保申报率明细Service业务层处理
 * 
 * @author xiongpeng
 * @date 2024-12-28
 */
@Service
public class SocialSecurityServiceImpl implements ISocialSecurityService
{

    private static final Logger log = LoggerFactory.getLogger(SocialSecurityServiceImpl.class);
    @Autowired
    private SocialSecurityMapper socialSecurityMapper;


    @Autowired
    protected Validator validator;

    /**
     * 查询居民医保申报率明细
     * 
     * @param id 居民医保申报率明细主键
     * @return 居民医保申报率明细
     */
    @Override
    public SocialSecurity selectSocialSecurityById(Long id)
    {
        return socialSecurityMapper.selectSocialSecurityById(id);
    }

    /**
     * 查询居民医保申报率明细列表
     * 
     * @param socialSecurity 居民医保申报率明细
     * @return 居民医保申报率明细
     */
    @Override
    public List<SocialSecurity> selectSocialSecurityList(SocialSecurity socialSecurity)
    {
        return socialSecurityMapper.selectSocialSecurityList(socialSecurity);
    }

    /**
     * 新增居民医保申报率明细
     * 
     * @param socialSecurity 居民医保申报率明细
     * @return 结果
     */
    @Override
    public int insertSocialSecurity(SocialSecurity socialSecurity)
    {
        return socialSecurityMapper.insertSocialSecurity(socialSecurity);
    }

    /**
     * 修改居民医保申报率明细
     * 
     * @param socialSecurity 居民医保申报率明细
     * @return 结果
     */
    @Override
    public int updateSocialSecurity(SocialSecurity socialSecurity)
    {
        socialSecurity.setUpdateTime(DateUtils.getNowDate());
        return socialSecurityMapper.updateSocialSecurity(socialSecurity);
    }

    /**
     * 批量删除居民医保申报率明细
     * 
     * @param ids 需要删除的居民医保申报率明细主键
     * @return 结果
     */
    @Override
    public int deleteSocialSecurityByIds(Long[] ids)
    {
        return socialSecurityMapper.deleteSocialSecurityByIds(ids);
    }

    /**
     * 删除居民医保申报率明细信息
     * 
     * @param id 居民医保申报率明细主键
     * @return 结果
     */
    @Override
    public int deleteSocialSecurityById(Long id)
    {
        return socialSecurityMapper.deleteSocialSecurityById(id);
    }

    @Override
    public String importUser(List<SocialSecurity> userList, boolean updateSupport, String operName)   {
        if (StringUtils.isNull(userList) || userList.size() == 0)
        {
            throw new ServiceException("导入社保数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (SocialSecurity user : userList)
        {
            try
            {
                // 验证是否存在这个用户
                SocialSecurity socialSecurity = new SocialSecurity();
                socialSecurity.setCertId(user.getCertId());
                List<SocialSecurity> securityList = socialSecurityMapper.selectSocialSecurityList(socialSecurity);
                SocialSecurity u = null;
                if(!CollectionUtils.isEmpty(securityList)){
                    u=securityList.get(0);
                }
                if (StringUtils.isNull(u))
                {
                    BeanValidators.validateWithException(validator, user);
                    socialSecurityMapper.insertSocialSecurity(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 导入成功");
                }
                else if (updateSupport)
                {
                    BeanValidators.validateWithException(validator, user);
                    socialSecurityMapper.updateSocialSecurity(user);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + user.getUserName() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getUserName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、账号 " + user.getUserName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
